package edu.infomatik.tundra.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.infomatik.tundra.data.DAOtundra;
import edu.infomatik.tundra.data.IBean;
import edu.infomatik.tundra.data.MockSprintBean;
import edu.infomatik.tundra.data.MockUStoryBean;
import edu.infomatik.tundra.logic.SprintBean;
import edu.infomatik.tundra.logic.SprintMetrics;
import edu.infomatik.tundra.logic.UStoryBean;
import edu.infomatik.tundra.logic.WorkBean;
import edu.infomatik.tundra.logic.WorkKinds;
import edu.infomatik.tundra.logic.security.SystemAccessBean;

@SuppressWarnings("unused")
public class TheStory {
	@SessionState
	private WorkBean project;
	private boolean projectExists;

	@SessionState
	private SystemAccessBean user;
	private boolean userExists;
	@SessionState
	private Boolean newWork=new Boolean(false);
	
	@SessionState
	@Property
	private UStoryBean story;
	private SprintBean sprint;
	private List<UStoryBean> storyList;
	private List<SprintBean> sprintList;
	
	@Inject
	private Messages message;
	
	public String getProjectName(){
		return project.getName();
	}
	
	public String getMetrics(){
		return message.get("SprintMetrics."+SprintMetrics.get(sprint.getMetrics()).getName());
	}
	
	public boolean isDemoShowing(){
		if (null==story.getDemo() || story.getDemo().length()<2) return false;
		return true;
	}

	public boolean isNoteShowing(){
		if (null==story.getNote() || story.getNote().length()<2) return false;
		return true;
	}
	public boolean isDescriptonShowing(){
		if (null==story.getDescription() || story.getDescription().length()<2) return false;
		return true;
	}	
	
	public String getLinkedWithName() {
		String result="";
		if (! storyList.isEmpty()){
			for (UStoryBean usb : storyList)
				if (story.getLinkedWith()==usb.getId())
					result=usb.getName();	
		}
		return result;
	}

	String onActivate(){
		if (!userExists) return "Index";
		else if (!projectExists) return "ProjectList"; 
		getAllStories();
		getAllSprints();
		return null;
	}
	
	public List<UStoryBean> getAllStories(){
		if (null == storyList) storyList=new ArrayList<UStoryBean>();
		if (storyList.isEmpty()){
			for (IBean usb: DAOtundra.getInstance().getAllBeans(new UStoryBean())){
				if (((UStoryBean)usb).getKind()==WorkKinds.USTORY_VALUE && ((UStoryBean)usb).getProjectId()==project.getId())
					storyList.add((UStoryBean)usb);
			}
		}
		return storyList;
	}

	public List<SprintBean> getAllSprints(){
		if (null == sprintList) sprintList=new ArrayList<SprintBean>();
		if (sprintList.isEmpty()){
			for (IBean sb: DAOtundra.getInstance().getAllBeans(new SprintBean())){
				if (((SprintBean)sb).getKind()==WorkKinds.SPRINT_VALUE)
					if (((SprintBean)sb).getProjectId()==project.getId())
						sprintList.add((SprintBean)sb);
			}
		}
		return sprintList;
	}

}
